# 诊断和修复数据库问题
Write-Host "=== 诊断和修复数据库问题 ===" -ForegroundColor Green

# 1. 检查数据库连接
Write-Host "`n1. 检查数据库连接..." -ForegroundColor Yellow
$connectionString = "server=47.119.112.235;database=soft1_admin;uid=postgres;pwd=h13178502607YY;"
Write-Host "连接字符串: $connectionString" -ForegroundColor Cyan
Write-Host "⚠ 需要手动检查PostgreSQL数据库连接" -ForegroundColor Yellow
Write-Host "请确认以下信息:" -ForegroundColor Yellow
Write-Host "  - PostgreSQL服务器: 47.119.112.235" -ForegroundColor White
Write-Host "  - 数据库名: soft1_admin" -ForegroundColor White
Write-Host "  - 用户名: postgres" -ForegroundColor White
Write-Host "  - 密码: h13178502607YY" -ForegroundColor White

# 2. 检查后端服务状态
Write-Host "`n2. 检查后端服务状态..." -ForegroundColor Yellow
try {
    $response = Invoke-RestMethod -Uri "http://localhost:5252/api/auth/validate" -Method GET -ErrorAction Stop
    Write-Host "✓ 后端服务正常运行" -ForegroundColor Green
} catch {
    Write-Host "✗ 后端服务连接失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "请确保后端服务正在运行" -ForegroundColor Yellow
}

# 3. 测试登录功能
Write-Host "`n3. 测试登录功能..." -ForegroundColor Yellow
try {
    $loginData = @{
        username = "admin"
        password = "password"
    } | ConvertTo-Json
    
    $loginResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/auth/login" -Method POST -Body $loginData -ContentType "application/json"
    
    if ($loginResponse.token) {
        Write-Host "✓ 登录成功，获取到Token" -ForegroundColor Green
        $token = $loginResponse.token
        Write-Host "Token: $token" -ForegroundColor Cyan
    } else {
        Write-Host "✗ 登录失败，未获取到Token" -ForegroundColor Red
    }
} catch {
    Write-Host "✗ 登录失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "可能的原因:" -ForegroundColor Yellow
    Write-Host "  1. 数据库中没有用户数据" -ForegroundColor White
    Write-Host "  2. 密码哈希不匹配" -ForegroundColor White
    Write-Host "  3. 数据库连接失败" -ForegroundColor White
}

# 4. 提供解决方案
Write-Host "`n4. 解决方案建议:" -ForegroundColor Yellow
Write-Host "如果登录失败，请尝试以下步骤:" -ForegroundColor White
Write-Host "  1. 检查PostgreSQL数据库是否运行" -ForegroundColor White
Write-Host "  2. 确认数据库连接字符串正确" -ForegroundColor White
Write-Host "  3. 执行数据库迁移: dotnet ef database update" -ForegroundColor White
Write-Host "  4. 检查数据库中是否有初始数据" -ForegroundColor White
Write-Host "  5. 如果数据库为空，可以手动插入测试数据" -ForegroundColor White

# 5. 创建测试数据脚本
Write-Host "`n5. 创建测试数据脚本..." -ForegroundColor Yellow
$testDataScript = @"
-- 测试数据插入脚本
-- 在PostgreSQL数据库中执行以下SQL

-- 插入测试用户
INSERT INTO "AppUsers" ("Id", "Username", "PasswordHash", "Email", "Phone", "FirstName", "LastName", "LastLoginAt", "CreatedAt", "UpdatedAt", "IsDeleted", "CreateBy", "UpdateBy", "RowVersion")
VALUES (
    '11111111-1111-1111-1111-111111111111',
    'admin',
    '$2a$11$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
    'admin@example.com',
    '+1234567890',
    'Admin',
    'User',
    '2025-07-31 06:34:05+00',
    '2025-07-31 06:34:05+00',
    '2025-07-31 06:34:05+00',
    false,
    'system',
    'system',
    '\x'
) ON CONFLICT ("Id") DO NOTHING;

-- 插入测试角色
INSERT INTO "AppRoles" ("Id", "Name", "Description", "CreatedAt", "UpdatedAt", "IsDeleted", "CreateBy", "UpdateBy", "RowVersion")
VALUES (
    '11111111-1111-1111-1111-111111111111',
    'Administrator',
    'System Administrator',
    '2025-07-31 06:34:05+00',
    '2025-07-31 06:34:05+00',
    false,
    'system',
    'system',
    '\x'
) ON CONFLICT ("Id") DO NOTHING;

-- 插入测试权限
INSERT INTO "AppPermissions" ("Id", "Code", "Name", "Description", "CreatedAt", "UpdatedAt", "IsDeleted", "CreateBy", "UpdateBy", "RowVersion")
VALUES (
    '11111111-1111-1111-1111-111111111111',
    'view_dashboard',
    'View Dashboard',
    'Permission to view the dashboard',
    '2025-07-31 06:34:05+00',
    '2025-07-31 06:34:05+00',
    false,
    'system',
    'system',
    '\x'
) ON CONFLICT ("Id") DO NOTHING;

-- 插入用户角色关联
INSERT INTO "AppUserRoles" ("Id", "UserId", "RoleId", "CreatedAt", "UpdatedAt", "IsDeleted", "CreateBy", "UpdateBy", "RowVersion")
VALUES (
    '11111111-1111-1111-1111-111111111111',
    '11111111-1111-1111-1111-111111111111',
    '11111111-1111-1111-1111-111111111111',
    '2025-07-31 06:34:05+00',
    '2025-07-31 06:34:05+00',
    false,
    'system',
    'system',
    '\x'
) ON CONFLICT ("Id") DO NOTHING;

-- 插入角色权限关联
INSERT INTO "AppRolePermissions" ("Id", "RoleId", "PermissionId", "CreatedAt", "UpdatedAt", "IsDeleted", "CreateBy", "UpdateBy", "RowVersion")
VALUES (
    '11111111-1111-1111-1111-111111111111',
    '11111111-1111-1111-1111-111111111111',
    '11111111-1111-1111-1111-111111111111',
    '2025-07-31 06:34:05+00',
    '2025-07-31 06:34:05+00',
    false,
    'system',
    'system',
    '\x'
) ON CONFLICT ("Id") DO NOTHING;
"@

$testDataScript | Out-File -FilePath "scripts/test-data.sql" -Encoding UTF8
Write-Host "✓ 测试数据脚本已创建: scripts/test-data.sql" -ForegroundColor Green

Write-Host "`n=== 诊断完成 ===" -ForegroundColor Green
Write-Host "请根据上述建议检查和修复数据库问题" -ForegroundColor Yellow 